/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package robocup.examples;

import robocup.messages.MessagesRobocupSslWrapper.SSL_WrapperPacket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

/**
 * This class is an example of following the ball with the received messages by SSLVision.
 * @author David Saldana
 */
public class BallFollower {

    private static final String IP_SERVER = "224.5.23.2";
    private static final int PORT = 10020;

    public static void main(String[] args) throws UnknownHostException, IOException {
        InetAddress group = InetAddress.getByName(IP_SERVER);
        MulticastSocket s = new MulticastSocket(PORT);
        s.joinGroup(group);

        // Define an udp packet to receive.
        byte[] buf = new byte[1000];
        DatagramPacket recv = new DatagramPacket(buf, buf.length);


        while (true) {
            // Receive an udp packet
            s.receive(recv);

            // Create a byte array with packet size.
            byte[] a = new byte[recv.getLength()];
            System.arraycopy(buf, 0, a, 0, recv.getLength());

            // Parse the received message
            SSL_WrapperPacket parsEFrom = SSL_WrapperPacket.parseFrom(a);


            // Print ball's position  
            System.out.println("x=" + parsEFrom.getDetection().getBalls(0).getX());
            System.out.println("y=" + parsEFrom.getDetection().getBalls(0).getY());
            System.out.println("z=" + parsEFrom.getDetection().getBalls(0).getZ());
        }
    }
}
